package com.hunter.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hunter.lease.common.exception.LeaseException;
import com.hunter.lease.common.result.Result;
import com.hunter.lease.common.result.ResultCodeEnum;
import com.hunter.lease.model.entity.ApartmentInfo;
import com.hunter.lease.model.entity.DistrictInfo;
import com.hunter.lease.web.admin.service.ApartmentInfoService;
import com.hunter.lease.web.admin.service.ApartmentService;
import com.hunter.lease.web.admin.service.DistrictInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ApartmentServiceImpl implements ApartmentService {

    @Autowired
    private DistrictInfoService districtInfoService;


    @Autowired
    private ApartmentInfoService apartmentInfoService;


    @Override
    public void checkDistrict(Long id) {

        // 区域ID校验

        if (id == null || id <= 0) {
            throw new LeaseException(ResultCodeEnum.DISTRICTID_ILLEGAL);
        }

        // 区域信息校验

        DistrictInfo districtInfo = districtInfoService.getById(id);
        if (districtInfo == null) {
            throw new LeaseException(ResultCodeEnum.DISTRICT_NULL);
        }

        // 区域下的公寓信息校验

        LambdaQueryWrapper<ApartmentInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApartmentInfo::getDistrictId, id);
        long count = apartmentInfoService.count(queryWrapper);
        if (count == 0) {
            throw new LeaseException(ResultCodeEnum.DISTRICTID_APARTMENTINFO_NULL);
        }

    }
}
